package com.cowcowit.springcloud.controller;

import com.cowcowit.springcloud.entity.Order;
import com.cowcowit.springcloud.result.ResultData;
import com.cowcowit.springcloud.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  订单控制器
 * </p>
 *
 * @author whboy
 * @since 2025-09-23
 */
@RestController
@RequestMapping("/springcloud/order")
public class OrderController {
    
    @Autowired
    private IOrderService orderService;
    /**
     * 创建订单
     * @param order 订单对象
     * @return 订单对象
     */
    @GetMapping("/create")
    public ResultData create(Order order) throws InterruptedException {
        orderService.create(order);
        return ResultData.success(order);
    }
    /**
     * 新增订单记录
     * @param order 订单对象
     * @return 操作结果
     */
    @PostMapping("/add")
    public ResultData<String> addOrder(@RequestBody Order order) {
        boolean result = orderService.save(order);
        if (result) {
            return ResultData.success("订单添加成功");
        } else {
            return ResultData.fail("500", "订单添加失败");
        }
    }
    
    /**
     * 根据ID删除订单记录
     * @param id 订单ID
     * @return 操作结果
     */
    @DeleteMapping("/delete/{id}")
    public ResultData<String> deleteOrderById(@PathVariable("id") Long id) {
        boolean result = orderService.removeById(id);
        if (result) {
            return ResultData.success("订单删除成功");
        } else {
            return ResultData.fail("500", "订单删除失败");
        }
    }
    
    /**
     * 更新订单记录
     * @param order 订单对象
     * @return 操作结果
     */
    @PostMapping("/update")
    public ResultData<String> updateOrder(@RequestBody Order order) {
        boolean result = orderService.updateById(order);
        if (result) {
            return ResultData.success("订单更新成功");
        } else {
            return ResultData.fail("500", "订单更新失败");
        }
    }
    
    /**
     * 根据ID获取订单信息
     * @param id 订单ID
     * @return 订单信息
     */
    @GetMapping("/get/{id}")
    public ResultData<Order> getOrderById(@PathVariable("id") Long id) {
        Order order = orderService.getById(id);
        return ResultData.success(order);
    }
    
    /**
     * 查询所有订单记录
     * @return 所有订单记录列表
     */
    @GetMapping("/list")
    public ResultData<List<Order>> getAllOrders() {
        List<Order> orders = orderService.list();
        return ResultData.success(orders);
    }
}